/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import DAO.AdminDAO;
import Models.Admin;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Cyro
 */
@ManagedBean
@RequestScoped
public class AdminController {

    public void checkValidUser() {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        Admin a1 = (Admin) (FacesContext.getCurrentInstance().getExternalContext().
                getRequestMap().get("adminBean"));
        //co gang lay ds tu session ra
        Admin a2 = new AdminDAO().getUser(a1);
        if (a1.getPassword().equals(a2.getPassword())) {
            try {
                session.setAttribute("admin", a2);
                FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(AdminController.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            FacesContext context = FacesContext.getCurrentInstance();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Information: ", "Please check your username or password!"));
        }
    }

    public void checkLogin() {
        try {
            ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
            HttpSession session = (HttpSession) e.getSession(true);
            if (session.getAttribute("admin") == null || session.getAttribute("admin").equals("")) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("login.xhtml");
            }
        } catch (Exception ex) {
        }
    }

    public void loginAgain() {
        try {
            ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
            HttpSession session = (HttpSession) e.getSession(true);
            if (session.getAttribute("admin") != null || !session.getAttribute("admin").equals("")) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
            }
        } catch (Exception ex) {
        }
    }

    public void processRegister() {
        Admin a = (Admin) (FacesContext.getCurrentInstance().getExternalContext().
                getRequestMap().get("adminBean"));
        new AdminDAO().addAdmin(a);
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("listAdmin.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(FeedBackController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void processLogout() {
        try {
            ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
            HttpSession session = (HttpSession) e.getSession(true);
            session.setAttribute("admin", null);
            FacesContext.getCurrentInstance().getExternalContext().redirect("login.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(AdminController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    ArrayList<Admin> listAdmin;

    public void selectedID(int selectedID) {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        session.setAttribute("selectedID", selectedID);
    }

    public ArrayList<Admin> getListAdmin() {
        listAdmin = new AdminDAO().getAllAdmin();
        return listAdmin;
    }

    public void deleteAdmin() {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        int id = Integer.parseInt(session.getAttribute("selectedID").toString());
        Admin a = new AdminDAO().getUserByID(id);
        if (a.getRole() != 1) {
            new AdminDAO().deleteAdminByID(id);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Information: ", "Admin has been deleted !"));
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Information: ", "You don't have permission to do this action !"));
        }
    }

    public void editAdmin(int id, String username, String password, String fullname, String address, String phone) throws Exception {
        Admin a = new AdminDAO().getUserByID(id);
        if (a.getRole() != 1) {
            new AdminDAO().deleteAdminByID(id);
            AdminDAO.editAdmin(id, username, password, fullname, address, phone);
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Information: ", "You don't have permission to do this action !"));
        }
    }

    /**
     * Creates a new instance of AdminController
     */
    public AdminController() {
    }
}
